<!---
 
  Copyright (c) 2010, Cristian Costantini - cristian@millemultimedia.it
  
  FileManager is licensed under the terms of the Open Source GPL 3.0 license. 
	
	http://www.gnu.org/licenses/gpl.html
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--->

<cfcomponent output="false" hint="I'm a File Manager core. ">

	<cfset variables.plugins = structNew() />

	<cffunction name="init" access="public" output="false" returntype="FileManager">
        <cfargument name="eventManager" type="fileManager.event.SystemEventManager" />
        <cfargument name="sessionMgr" type="fileManager.util.SessionManager" />
        <cfargument name="plugInLoader" type="fileManager.core.PlugInLoader" />
        <cfargument name="displayObject" type="fileManager.util.DisplayObject" />
		
		<cfscript>
		variables.eventManager = arguments.eventManager ;
		variables.sessionMgr = arguments.sessionMgr;
		variables.plugInLoader = arguments.plugInLoader;
		variables.displayObject = arguments.displayObject;
		
		variables.plugInLoader.loadPlugin();
		
		variables.eventManager.notifyOnApplicationStart();
		</cfscript>
		
		<cfreturn this />
	</cffunction>
	
	<cffunction name="onPluginLoad" access="public" returntype="void">
		<cfargument name="name" type="string" required="true" hint="Plugin name" />
		<cfargument name="plugin" type="fileManager.util.AbsPlugin" required="true" hint="Plugin instance" />
		<cfargument name="pluginConfig" type="xml" required="true" hint="Plugin config" />
		
		
		<cfset registerPlugin( arguments.name, arguments.plugin, arguments.pluginConfig ) />
		
	</cffunction>
	
	<cffunction name="invokeServiceMethod" access="public" returntype="Any" >
		<cfargument name="name" type="string" required="true" hint="The service name" />
		<cfargument name="methodName" type="string" required="true" />
		<cfargument name="args" type="struct" required="true" />
		<cfset var result = "" />
		<cfset var target = getService( arguments.name ) />
		
		<cfinvoke component="#target#"
					  method="#arguments.methodName#" 
					  argumentcollection="#arguments.args#" 
					  returnvariable="result">
					  
		<cfreturn result />
	</cffunction>
	
	<!--- public --->
	
	<cffunction name="registerPlugin" access="private" returntype="void">
		<cfargument name="name" type="string" required="true" hint="Plugin name" />
		<cfargument name="plugin" type="fileManager.util.AbsPlugin" required="true" hint="Plugin instance" />
		<cfargument name="pluginConfig" type="xml" required="true" hint="Plugin config" />
		
		<cfset variables.plugins.put( arguments.name, arguments.plugin ) />
		
		<cfset variables.eventManager.notifyonPluginRegister( argumentCollection=arguments ) />
	</cffunction>
	
	<cffunction name="getService" access="public" returntype="Component">
		<cfargument name="name" type="string" required="true" hint="Plugin name" />
		
		<cfreturn variables.plugins.get( arguments.name ).getService() />
	</cffunction>
	
	<cffunction name="getDSPobjects" access="public" returntype="fileManager.util.DisplayObject">
		<cfreturn variables.displayObject />
	</cffunction>
	
</cfcomponent>